﻿using System;
using System.Windows.Forms; 
using System.Drawing;       

namespace Boggle {

    /// <summary>
    /// Represent the game board tiles and locations.
    /// <BR>
    /// Author: David I. Schwartz (dis@mail.rit.edu)
    /// <BR>
    /// </summary>
    public class Board {

        #region fields
        private Button[,] buttons;                           // buttons on game board (tiles)
        private GridLocation[,] locations;                   // corresponding positions of tiles
        public static Color defaultColor = Color.Turquoise;  // default color (unselected)
        public static Color newestColor = Color.Yellow;      // most recent selection color
        public static Color olderColor = Color.Pink;         // previous selection color
        #endregion fields

        #region constructor
        /// <summary>
        /// Create the game board object given previously made buttons and locations.
        /// </summary>
        /// <param name="buttons">Matrix of buttons.</param>
        /// <param name="grid">Matrix of button locations.</param>
        public Board(Button[,] buttons, GridLocation[,] grid) {
            this.buttons = buttons;
            this.locations = grid;
        }
        #endregion constructor

        #region access
        /// <summary>
        /// Return board's button matrix.
        /// </summary>
        public Button[,] Buttons {
            get { return buttons; }
        }

        /// <summary>
        /// Access a particular game button.
        /// </summary>
        /// <param name="row">row position of button</param>
        /// <param name="col">column position of button</param>
        /// <returns>corresponding game button at (row,col)</returns>
        public Button this[int row, int col] {
            get { return buttons[row,col]; }
            set { buttons[row,col] = value; }
        }

        /// <summary>
        /// Return the location of a particular board button.
        /// </summary>
        /// <param name="b">input button</param>
        /// <returns>corresponding position</returns>
        public GridLocation this[Button b] {
            get {
                int tmpRow = -1;
                int tmpCol = -1;
                for (int row = 0; row < BoggleWindow.ROWS; row++) {
                    for (int col = 0; col < BoggleWindow.COLS; col++) {
                        if (buttons[row, col].Equals(b)) {
                            tmpRow = row;
                            tmpCol = col;
                        }
                    }
                }
                return new GridLocation(tmpRow, tmpCol);
            }

        }
        #endregion access

        #region appearance
        /// <summary>
        /// Set current Button to most recent selection color.
        /// </summary>
        public void setNewest(Button b) {
            b.BackColor = newestColor;
        }

        /// <summary>
        /// Set current DieButton to most default color.
        /// </summary>
        public void deSelect(Button b) {
            b.BackColor = defaultColor;
        }

        /// <summary>
        /// Set Button to "previous selection color" 
        /// (this die was selected but not the most current selected DieButton).
        /// </summary>
        public void setOlder(Button b) {
            b.BackColor = olderColor;
        }

        /// <summary>
        /// Set Button to default color and blank letter.
        /// </summary>
        public void reset(Button b) {
            b.BackColor = defaultColor;
            b.Text = ""; // careful -- we want might to keep old letter
            b.Enabled = true;
        }

        /// <summary>
        /// Enable each Button and set default color, but keep current letter.
        /// </summary>
        public void resetAllLetters() {
            for (int row = 0; row < BoggleWindow.ROWS; row++) {
                for (int col = 0; col < BoggleWindow.COLS; col++) {
                    buttons[row, col].BackColor = defaultColor;
                    buttons[row, col].Enabled = true;
                }
            }
        }

        /// <summary>
        /// Completely refresh tiles.
        /// </summary>
        public void resetBlanks() {
            for (int row = 0; row < BoggleWindow.ROWS; row++) {
                for (int col = 0; col < BoggleWindow.COLS; col++) {
                    buttons[row, col].BackColor = defaultColor;
                    buttons[row, col].Enabled = true;
                    buttons[row, col].Text = "";
                }
            }
        }

        #endregion appearance

        #region checks

        /// <summary>
        /// Check if supplied DieButton is a clickable neighbor of current DieButton
        /// (1 space away in N, NE, E, SE, S, SW, and W directions).
        /// </summary>
        /// <param name="other">supplied DieButton</param>
        /// <returns>whether or not supplied button is a legal neighbor</returns>
        public bool withinRange(Button current, Button other) {

            int row1 = this[current].Row;
            int col1 = this[current].Col;
            int row2 = this[other].Row;
            int col2 = this[other].Col;

            return
                ( (row2 == row1 - 1) &&  // row above current button
                    ( (col2 == col1 - 1) || (col2 == col1) || (col2 == col1 + 1) ) ) ||

                ( (row2 == row1) &&      // same row as current button
                    (( col2 == col1 - 1)  || (col2 == col1 + 1) ) ) ||

                ( (row2 == row1 + 1) &&  // row below current button
                    ( (col2 == col1 - 1) || (col2 == col1) || (col2 == col1 + 1) ) );

        } // method withinRange

        #endregion checks

    } // class Board

} // Boggle

